package com.example.httpmode;

import static com.example.httpmode.R.mipmap.off;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

public class Bedroom1Activity extends AppCompatActivity implements View.OnClickListener {

    private ImageView bedroom1_iv_exit;
    private TextView bedroom1_tv_temp;
    private TextView bedroom1_tv_hum;
    private TextView bedroom1_tv_light;
    private TextView bedroom1_tv_body;
    private TextView bedroom1_tv_bodys;
    private TextView bedroom1_tv_flame;
    private TextView bedroom1_tv_smoke;
    private String token;
    private SharedPreferences config;
    private String ip;
    private String port;
    private ImageView bedroom1_iv_automatic;
    private ImageView bedroom1_iv_fan;
    private ImageView bedroom1_iv_alarm;
    private ImageView bedroom1_iv_lamp;
    private int automatic_value = 0;
    private int fan_value = 0;
    private int alarm_value = 0;
    private int lamp_value = 0;
    private String bedroom1_gateway;
    private String police;
    private String fan;
    private String lamp;
    private int bedroom1fandtate = 0;
    private int bedroom1policedtate = 0;
    private int bedroom1lampdtate = 0;
    private int bedroom1dtatefan = 0;
    private int bedroom1dtatepolice = 0;
    private int bedroom1dtatelamp = 0;
    private int bedroom1automatic = 0;
    private int bedroom1automaticstatic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bedroom1);

        //查找控件
        bedroom1_iv_exit = findViewById(R.id.bedroom1_iv_exit);
        bedroom1_tv_temp = findViewById(R.id.bedroom1_tv_temp);
        bedroom1_tv_hum = findViewById(R.id.bedroom1_tv_hum);
        bedroom1_tv_light = findViewById(R.id.bedroom1_tv_light);
        bedroom1_tv_body = findViewById(R.id.bedroom1_tv_body);
        bedroom1_tv_bodys = findViewById(R.id.bedroom1_tv_bodys);
        bedroom1_tv_flame = findViewById(R.id.bedroom1_tv_flame);
        bedroom1_tv_smoke = findViewById(R.id.bedroom1_tv_smoke);

        bedroom1_iv_automatic = findViewById(R.id.bedroom1_iv_automatic);
        bedroom1_iv_fan = findViewById(R.id.bedroom1_iv_fan);
        bedroom1_iv_alarm = findViewById(R.id.bedroom1_iv_alarm);
        bedroom1_iv_lamp = findViewById(R.id.bedroom1_iv_lamp);

        //监听
        bedroom1_iv_exit.setOnClickListener(this);
        bedroom1_iv_automatic.setOnClickListener(this);
        bedroom1_iv_fan.setOnClickListener(this);
        bedroom1_iv_alarm.setOnClickListener(this);
        bedroom1_iv_lamp.setOnClickListener(this);

        //获取token
        config = getSharedPreferences("config", MODE_PRIVATE);
        token = config.getString("token", "");
        //System.out.println(token);
        //从config文件中获取ip,端口，网关id,以及传感器标识
        ip = config.getString("ip", "");
        port = config.getString("port", "");
        bedroom1_gateway = config.getString("bedroom1_gateway", "");
        String temp = config.getString("setting_temp", "");
        String hum = config.getString("setting_hum", "");
        String light = config.getString("setting_light", "");
        String body = config.getString("setting_body", "");
        String bodys = config.getString("setting_bodys", "");
        String flame = config.getString("setting_flame", "");
        String smoke = config.getString("setting_smoke", "");
        police = config.getString("setting_police", "");
        fan = config.getString("setting_fan", "");
        lamp = config.getString("setting_lamp", "");

        if(config != null && !"".equals(config)){
            System.out.println("状态已获取成功");
            //自动控制状态获取
            bedroom1automaticstatic = config.getInt("bedroom1automatic", 0);
            //空调状态获取
            bedroom1dtatefan = config.getInt("bedroom1fandtate",0);
            //报警灯状态获取
            bedroom1dtatepolice = config.getInt("bedroom1policedtate",0);
            //大灯状态获取
            bedroom1dtatelamp = config.getInt("bedroom1lampdtate",0);

            if(bedroom1automaticstatic == 1){
                bedroom1_iv_automatic.setImageResource(off);
                bedroom1_iv_fan.setEnabled(false);
                bedroom1_iv_alarm.setEnabled(false);
                bedroom1_iv_lamp.setEnabled(false);
            }else if(bedroom1automaticstatic == 2){
                bedroom1_iv_automatic.setImageResource(R.mipmap.no);
                bedroom1_iv_fan.setEnabled(true);
                bedroom1_iv_alarm.setEnabled(true);
                bedroom1_iv_lamp.setEnabled(true);
            }

            if(bedroom1dtatefan == 1){
                bedroom1_iv_fan.setImageResource(off);
                System.out.println("空调状态为打开");
            }else if(bedroom1dtatefan == 0){
                bedroom1_iv_fan.setImageResource(R.mipmap.no);
                System.out.println("空调状态为关闭");
            }
            if(bedroom1dtatelamp == 1){
                bedroom1_iv_lamp.setImageResource(off);
                System.out.println("大灯状态为打开");
            }else if(bedroom1dtatelamp == 0){
                bedroom1_iv_lamp.setImageResource(R.mipmap.no);
                System.out.println("大灯状态为关闭");
            }
            if(bedroom1dtatepolice == 1){
                bedroom1_iv_alarm.setImageResource(off);
                System.out.println("报警灯状态为打开");
            }else if(bedroom1dtatepolice == 0){
                bedroom1_iv_alarm.setImageResource(R.mipmap.no);
                System.out.println("报警灯状态为关闭");
            }
        }else{
            System.out.println("状态获取失败........");
        }

        //Log.e("***************************",flame);
        //数据获取 -- > 子线程网络请求

        new Thread(){//温度
            @Override
            public void run() {
                while(true){
                    String temp_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ temp;
                    String temp_json = HttpUrlConnetc.get(temp_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(temp_json);
                        JSONObject ResultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = ResultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String temp_value = jsonObject2.getString("Value");
                        System.out.println("温度:"+temp_value);
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 1;

                        Bundle bundle = new Bundle();
                        bundle.putString("temp_value",temp_value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


        new Thread(){//湿度
            @Override
            public void run() {
                while(true){
                    String temp_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ hum;
                    String temp_json = HttpUrlConnetc.get(temp_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(temp_json);
                        JSONObject ResultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = ResultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String hum_value = jsonObject2.getString("Value");
                        //System.out.println("湿度:"+hum_value);
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 2;

                        Bundle bundle = new Bundle();
                        bundle.putString("hum_value",hum_value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){//光照
            @Override
            public void run() {
                while(true){
                    String light_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ light;
                    String light_json = HttpUrlConnetc.get(light_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(light_json);
                        JSONObject ResultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = ResultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String light_value = jsonObject2.getString("Value");
                        //System.out.println("光照:"+light_value);
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 3;

                        Bundle bundle = new Bundle();
                        bundle.putString("light_value",light_value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


        new Thread(){//人体
            @Override
            public void run() {
                while(true){
                    String body_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ body;
                    String body_json = HttpUrlConnetc.get(body_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(body_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String body_value = jsonObject2.getString("Value");


                        //延时5秒
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 4;

                        Bundle bundle = new Bundle();
                        bundle.putString("body_value",body_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){//微波
            @Override
            public void run() {
                while(true){
                    String bodys_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ bodys;
                    String bodys_json = HttpUrlConnetc.get(bodys_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(bodys_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String bodys_value = jsonObject2.getString("Value");

                        //Log.e("微波",bodys_value);

                        //延时5秒
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 5;

                        Bundle bundle = new Bundle();
                        bundle.putString("bodys_value",bodys_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){//火焰
            @Override
            public void run() {
                while(true){
                    String flame_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ flame;
                    String flame_json = HttpUrlConnetc.get(flame_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(flame_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String flame_value = jsonObject2.getString("Value");

                        //Log.e("火焰",flame_value);

                        //延时5秒
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 6;

                        Bundle bundle = new Bundle();
                        bundle.putString("flame_value",flame_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


        new Thread(){//烟雾
            @Override
            public void run() {
                while(true){
                    String smoke_url = "http://"+ ip +":"+ port +"/devices/"+ bedroom1_gateway +"/Datas?deviceId="+ bedroom1_gateway +"&ApiTags="+ smoke;
                    String smoke_json = HttpUrlConnetc.get(smoke_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(smoke_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                        String smoke_value = jsonObject2.getString("Value");

                        //Log.e("烟雾",smoke_value);
                        //Log.i("","代码测试中，请稍后..................");

                        //延时5秒
                        Thread.sleep(5000);

                        Message message = new Message();
                        message.what = 7;

                        Bundle bundle = new Bundle();
                        bundle.putString("smoke_value",smoke_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.bedroom1_iv_exit:
                finish();
                overridePendingTransition(0,0);
                break;
            case R.id.bedroom1_iv_automatic:
                bedroom1automaticstatic++;
                if(bedroom1automaticstatic == 1){
                    //自动控制打开，按键失灵
                    System.out.println("打开自动控制");
                    bedroom1_iv_automatic.setImageResource(off);
                    bedroom1_iv_fan.setEnabled(false);
                    bedroom1_iv_alarm.setEnabled(false);
                    bedroom1_iv_lamp.setEnabled(false);
                    bedroom1automatic = 1;//自动控制打开
                    SharedPreferences.Editor edit1 = config.edit();
                    edit1.putInt("bedroom1automatic",bedroom1automatic);
                    edit1.commit();
                }else if(bedroom1automaticstatic == 2){
                    //自动控制关闭，按键恢复正常
                    System.out.println("关闭自动控制");
                    bedroom1_iv_automatic.setImageResource(R.mipmap.no);
                    bedroom1_iv_fan.setEnabled(true);
                    bedroom1_iv_alarm.setEnabled(true);
                    bedroom1_iv_lamp.setEnabled(true);
                    automatic_value = 0;
                    bedroom1automaticstatic = 0;
                    bedroom1automatic = 0;//自动控制关闭
                    SharedPreferences.Editor edit1 = config.edit();
                    edit1.putInt("bedroom1automatic",bedroom1automatic);
                    edit1.commit();
                }
                break;

            case R.id.bedroom1_iv_fan:
                bedroom1dtatefan++;
                if(bedroom1dtatefan == 1){
                    System.out.println("打开空调");
                    bedroom1_iv_fan.setImageResource(off);
                    //子线程网络请求
                    //打开空调
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + fan;
                            HttpUrlConnetc.post(fan_url,"1",token);
                        }
                    }.start();
                    bedroom1fandtate = 1;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1fandtate",bedroom1fandtate);
                    edit.commit();
                }else if(bedroom1dtatefan == 2){
                    System.out.println("关闭空调");
                    bedroom1_iv_fan.setImageResource(R.mipmap.no);
                    fan_value = 0;
                    //关闭空调
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + fan;
                            HttpUrlConnetc.post(fan_url,"0",token);
                        }
                    }.start();
                    bedroom1dtatefan = 0;
                    bedroom1fandtate = 0;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1fandtate",bedroom1fandtate);
                    edit.commit();
                }
                break;

            case R.id.bedroom1_iv_alarm:
                bedroom1dtatepolice++;
                if(bedroom1dtatepolice == 1){
                    System.out.println("打开报警灯");
                    bedroom1_iv_alarm.setImageResource(off);
                    //打开报警灯
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + police;
                            HttpUrlConnetc.post(fan_url,"1",token);
                        }
                    }.start();
                    bedroom1policedtate = 1;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1policedtate",bedroom1policedtate);
                    edit.commit();
                }else if(bedroom1dtatepolice == 2){
                    System.out.println("关闭报警灯");
                    bedroom1_iv_alarm.setImageResource(R.mipmap.no);
                    alarm_value = 0;
                    //关闭报警灯
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + police;
                            HttpUrlConnetc.post(fan_url,"0",token);
                        }
                    }.start();
                    bedroom1dtatepolice = 0;
                    bedroom1policedtate = 0;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1policedtate",bedroom1policedtate);
                    edit.commit();
                }
                break;

            case R.id.bedroom1_iv_lamp:
                bedroom1dtatelamp++;
                if(bedroom1dtatelamp == 1){
                    System.out.println("打开大灯");
                    bedroom1_iv_lamp.setImageResource(off);
                    //打开大灯
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + lamp;
                            HttpUrlConnetc.post(fan_url,"1",token);
                        }
                    }.start();
                    bedroom1lampdtate = 1;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1lampdtate",bedroom1lampdtate);
                    edit.commit();
                }else if(bedroom1dtatelamp == 2){
                    System.out.println("关闭大灯");
                    bedroom1_iv_lamp.setImageResource(R.mipmap.no);
                    lamp_value = 0;
                    //关闭大灯
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String fan_url = "http://" + ip +"/Cmds?deviceId="+ bedroom1_gateway + "&apiTag=" + lamp;
                            HttpUrlConnetc.post(fan_url,"0",token);
                        }
                    }.start();
                    bedroom1dtatelamp = 0;
                    bedroom1lampdtate = 0;
                    SharedPreferences.Editor edit = config.edit();
                    edit.putInt("bedroom1lampdtate",bedroom1lampdtate);
                    edit.commit();
                }
                break;
        }
    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch(msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String temp_value = data.getString("temp_value");
                    bedroom1_tv_temp.setText(temp_value);
                    break;
                case 2:
                    Bundle data1 = msg.getData();
                    String hum_value = data1.getString("hum_value");
                    bedroom1_tv_hum.setText(hum_value);
                    break;
                case 3:
                    Bundle data2 = msg.getData();
                    String light_value = data2.getString("light_value");
                    bedroom1_tv_light.setText(light_value);
                    break;
                case 4:
                    Bundle data3 = msg.getData();
                    String body_value = data3.getString("body_value");
                    bedroom1_tv_body.setText(body_value);
                    break;
                case 5:
                    Bundle data4 = msg.getData();
                    String bodys_value = data4.getString("bodys_value");
                    bedroom1_tv_bodys.setText(bodys_value);
                    break;
                case 6:
                    Bundle data5 = msg.getData();
                    String flame_value = data5.getString("flame_value");
                    bedroom1_tv_flame.setText(flame_value);
                    break;
                case 7:
                    Bundle data6 = msg.getData();
                    String smoke_value = data6.getString("smoke_value");
                    bedroom1_tv_smoke.setText(smoke_value);
                    break;
            }
        }
    };
}